Day 11 - Find and replace text
37
$ grep "red" examples.txt
undead red dragon
If you want to find only the matches that cover the whole line use the -x option
$ grep "dog" examples.txt
dog
dog
corn dog
$ grep -x "dog" examples.txt
dog
dog
By default grep performs a case-sensitive search, so lowercase and uppercase are meaningful. If you
want to perform a case-insensitive search use the -i option
$ grep "H" examples.txt
H2O
HTTP/1.1
$ grep -i "h" examples.txt
elephant
ostrich
Dug the Dog
beholder
H2O
phase spider
Johnny 5
hogwash
wild hog
HTTP/1.1
hog
Once we find what we need, or when we have a certain text in a variable, we often want to change
it, replacing parts of it with something different. The sed utility can do this (and actually many other
things). Try to execute
$ grep "elephant" examples.txt | sed s,"ele","oli",
oliphant
which replaces the string “ele” with the string “oli”. The s command of sed needs to receive the
search and the replacement patters surrounded by a character that is not part of them. In this case
I used the comma, but you will find many times the /, which is another typical character used for
this tool